package com.kye.express.business.jd.order_detail;

import com.kye.express.business.waybill.waybill_detail.RouterStatusTimeLineView;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.jd.order_details.JDOrderRouterInfoBean;
import com.kye.model.bean.logistics.RouterStatusViewType;
import com.kye.model.bean.user.User;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public interface JDOrderDetailRouterInfoConstract {

    interface JDOrderDetailRouterInfoView extends IView {

        void onUser(User user);

        void initRouterView(List<RouterStatusTimeLineView.RouterStatusTimeLineModel> routerStatusTimeLineModels);
    }

    class IntegralOrderJDRouterInfoPresenter extends BasePresenter<JDOrderDetailRouterInfoView> {

        @Override
        public void bind(JDOrderDetailRouterInfoView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void initRouterData(ArrayList<JDOrderRouterInfoBean> routerInfoBeans) {
            Observable
                    .create(new ObservableOnSubscribe<ArrayList<JDOrderRouterInfoBean>>() {
                        @Override
                        public void subscribe(ObservableEmitter<ArrayList<JDOrderRouterInfoBean>> e) throws Exception {
                            Collections.reverse(routerInfoBeans);
                            e.onNext(routerInfoBeans);
                            e.onComplete();
                        }
                    })
                    .flatMap(new Function<ArrayList<JDOrderRouterInfoBean>, ObservableSource<JDOrderRouterInfoBean>>() {
                        @Override
                        public ObservableSource<JDOrderRouterInfoBean> apply(ArrayList<JDOrderRouterInfoBean> jdOrderRouterInfoBeans) throws Exception {
                            return Observable.fromIterable(jdOrderRouterInfoBeans);
                        }
                    })
                    .map(new Function<JDOrderRouterInfoBean, RouterStatusTimeLineView.RouterStatusTimeLineModel>() {
                        @Override
                        public RouterStatusTimeLineView.RouterStatusTimeLineModel apply(JDOrderRouterInfoBean jdOrderRouterInfoBean) throws Exception {
                            RouterStatusTimeLineView.RouterStatusTimeLineModel timeLineModel = new RouterStatusTimeLineView.RouterStatusTimeLineModel();
                            timeLineModel.setTime(jdOrderRouterInfoBean.getMsgTime());
                            timeLineModel.setFinished(true);
                            timeLineModel.setMessage(jdOrderRouterInfoBean.getContent());
                            return timeLineModel;
                        }
                    })
                    .toList()
                    .doOnSuccess(new Consumer<List<RouterStatusTimeLineView.RouterStatusTimeLineModel>>() {
                        @Override
                        public void accept(List<RouterStatusTimeLineView.RouterStatusTimeLineModel> routerStatusTimeLineModels) throws Exception {
                            // 0 : 完成节点
                            // size - 1 : 开始节点
                            int size = routerStatusTimeLineModels.size();
                            if (size == 1) {
                                routerStatusTimeLineModels.get(0).setType(RouterStatusViewType.TYPE_ONLY_ONE);
                            } else {
                                for (int i = 0; i < size; i++) {
                                    if (i == 0) {
                                        // begin
                                        routerStatusTimeLineModels.get(i).setType(RouterStatusViewType.TYPE_END);
                                    } else if (i == size - 1) {
                                        // end
                                        routerStatusTimeLineModels.get(i).setType(RouterStatusViewType.TYPE_BEGIN);
                                    } else {
                                        // normal
                                        routerStatusTimeLineModels.get(i).setType(RouterStatusViewType.TYPE_NORMAL);
                                    }
                                }
                            }
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<List<RouterStatusTimeLineView.RouterStatusTimeLineModel>>() {
                        @Override
                        public void accept(List<RouterStatusTimeLineView.RouterStatusTimeLineModel> routerStatusTimeLineModels) throws Exception {
                            getView().initRouterView(routerStatusTimeLineModels);
                        }
                    });


        }
    }
}
